﻿using System;


namespace ExtensibleEventViewer.Core.Primitives
{
	/// <summary>
	/// Represents single Event Log entry
	/// </summary>
	[Serializable]
	public class EventLogRecord
	{
		/// <summary>
		/// Initializes record instance
		/// </summary>
		/// <param name="id">Unique identifier of the event</param>
		/// <param name="computerName">Computer name that logged this event</param>
		/// <param name="source">Name of the application which generated this event</param>
		/// <param name="timeGenerated">Time when current record was generated</param>
		/// <param name="description">Description associated with this event</param>
		public EventLogRecord(object id, string computerName, string source, DateTime timeGenerated, string description) :
			this(id, computerName, source, timeGenerated, description, EventLogRecordType.Unknown, null, null)
		{
		}

		/// <summary>
		/// Initializes record instance
		/// </summary>
		/// <param name="id">Unique identifier of the event</param>
		/// <param name="computerName">Name of the computer that logged this event</param>
		/// <param name="source">Name of the application which generated this event</param>
		/// <param name="timeGenerated">Time when current record was generated</param>
		/// <param name="description">Description associated with this event</param>
		/// <param name="type">Event type</param>
		/// <param name="category">Category name</param>
		/// <param name="userName">Name of the user who is responsible for this event</param>
		public EventLogRecord(
			object id,
			string computerName,
			string source,
			DateTime timeGenerated,
			string description,
			EventLogRecordType type,
			string category,
			string userName)
		{
			ID = id;
			ComputerName = computerName;
			Description = description;
			Source = source;
			TimeGenerated = timeGenerated;
			Type = type;
			Category = category;
			UserName = userName;
		}

		/// <summary>
		/// Gets unique identifier of the event
		/// </summary>
		public object ID
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets name of the computer that logged this event
		/// </summary>
		public string ComputerName
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets event type
		/// </summary>
		public EventLogRecordType Type
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets the at which time current record was generated
		/// </summary>
		public DateTime TimeGenerated
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets the name of the application which generated this log record
		/// </summary>
		public string Source
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets description associated with current event record
		/// </summary>
		public string Description
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets event record category name
		/// </summary>
		public string Category
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets name of the user who is responsible for this event
		/// </summary>
		public string UserName
		{
			get; 
			private set;
		}

		public override int GetHashCode()
		{
			return (ID + ComputerName + Type + TimeGenerated + Source + Description + Category + UserName).GetHashCode();
		}

		public override bool Equals(object obj)
		{
			if (!(obj is EventLogRecord))
			{
				return false;
			}

			EventLogRecord record = (EventLogRecord) obj;

			if (Equals(record.ID, ID))
			{
				if(string.Equals(record.ComputerName, ComputerName, StringComparison.InvariantCultureIgnoreCase))
				{
					if(Type == record.Type)
					{
						if (TimeGenerated.Equals(record.TimeGenerated))
						{
							if (string.Equals(record.Source, Source, StringComparison.InvariantCultureIgnoreCase))
							{
								if (string.Equals(record.Description, Description, StringComparison.InvariantCultureIgnoreCase))
								{
									if (string.Equals(record.Category, Category, StringComparison.InvariantCultureIgnoreCase))
									{
										if (string.Equals(record.UserName, UserName, StringComparison.InvariantCultureIgnoreCase))
										{
											return true;
										}
									}
								}
							}
						}
					}
				}
			}

			return false;
		}
	}
}
